package com.example.mobile1920605116cxb0603;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private Button btnMainPlayer;
    private Button btnMainPause;
    private Button btnMainReplay;
    private Button btnMainStop;
    private MediaPlayer mediaPlayer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initMusic();
        btnMainPlayer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mediaPlayer==null){
                    mediaPlayer=MediaPlayer.create(MainActivity.this,R.raw.old_boy);
                }
                mediaPlayer.start();
            }
        });
        btnMainPause.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mediaPlayer!=null&&mediaPlayer.isPlaying()){
                    mediaPlayer.pause();
                }
            }
        });
        btnMainReplay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mediaPlayer!=null&&mediaPlayer.isPlaying()){
                    mediaPlayer.seekTo(0);
                    mediaPlayer.start();
                }
            }
        });
        btnMainStop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mediaPlayer.isPlaying()){
                    mediaPlayer.stop();
                    mediaPlayer.release();
                    mediaPlayer=null;
                }
            }
        });
    }

    private void initMusic() {
        mediaPlayer=MediaPlayer.create(this,R.raw.old_boy);
    }

    private void initView() {
        btnMainPlayer = (Button) findViewById(R.id.btnMainPlayer);
        btnMainPause = (Button) findViewById(R.id.btnMainPause);
        btnMainReplay = (Button) findViewById(R.id.btnMainReplay);
        btnMainStop = (Button) findViewById(R.id.btnMainStop);
    }
}
